package com.advantage.accounts.transfer;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.advantage.R;
import com.advantage.accounts.transactions.TransactionActivity;

/**
 * Created with IntelliJ IDEA.
 * User: hirsche
 * Date: 13/04/14
 * Time: 14:42
 * To change this template use File | Settings | File Templates.
 */
public class MoneyTransferFragment extends Fragment {

    public static final String FROM_ACCOUNT = "fromAccount";
    public static final String TO_ACCOUNT = "toAccount";
    public static final String AMOUNT = "amount";
    public static final String DATE_OF_TRANSACTION = "dateOfTrans";

    ListView accountsListView;
    private String[] accounts;

    public MoneyTransferFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_money_transfer, container, false);

        final Spinner spinnerFrom = (Spinner) rootView.findViewById(R.id.from_account_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapterFrom = ArrayAdapter.createFromResource(getActivity(),
                R.array.accounts_array, android.R.layout.simple_spinner_item);

// Specify the layout to use when the list of choices appears
        adapterFrom.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinnerFrom.setAdapter(adapterFrom);


        final Spinner spinnerTo = (Spinner) rootView.findViewById(R.id.to_account_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapterTo = ArrayAdapter.createFromResource(getActivity(),
                R.array.accounts_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapterTo.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinnerTo.setAdapter(adapterTo);


        Button nextButton = (Button) rootView.findViewById(R.id.next_button);
        nextButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String selectedItemFrom = (String)spinnerFrom.getSelectedItem();
                String selectedItemTo = (String)spinnerTo.getSelectedItem();
                if (selectedItemFrom.equals(selectedItemTo)) {
                    Toast.makeText(getActivity(),"Accounts can't be the same",Toast.LENGTH_SHORT).show();
                } else {
                   Intent intent = new Intent(v.getContext(),MoneyTransferActivity.class);
                   intent.putExtra(FROM_ACCOUNT,selectedItemFrom);
                   intent.putExtra(TO_ACCOUNT,selectedItemTo);
                   getActivity().startActivity(intent);
                }
            }
        });
        return rootView;
    }



}
